<?php
/**
 * group_chats
 *
 * @package group_chat
 *
 */
elgg_register_event_handler ( 'init', 'system', 'dataentry_init' );

/**
 * Init group_chat plugin.
 */
function dataentry_init() {
	elgg_register_plugin_hook_handler ( 'register', 'menu:owner_block', 'dataentry_owner_block_menu' );
	
	$action_base = elgg_get_plugins_path () . "/subjectmanage/actions";
	elgg_register_action ( 'addgroupsubject', "$action_base/addgroupsubject.php" );
	elgg_register_action ( 'addlistproject', "$action_base/addlistproject.php" );
	elgg_register_action ( 'registerauto', "$action_base/registerauto.php" );
	// elgg_register_action ( 'assigntask', "$action_base/assigntask.php" );
	// elgg_register_action ( 'submitasg', "$action_base/submitasg.php" );
	
	elgg_register_page_handler ( 'dataentry', 'dataentry_page_handler' );
}

// Register menu items for group profile
function dataentry_owner_block_menu($hook, $type, $return, $params) {
	$user = elgg_get_logged_in_user_entity ();
	if (elgg_instanceof ( $params ['entity'], 'group' )) {
		// $url = "dataentry/addsvncommit/{$params ['entity']->guid}";
		// $item = new ElggMenuItem ( 'groupsubject', elgg_echo ( "Tạo svn commit/update" ), $url );
		
		// $return [] = $item;
		$group = $params ['entity'];
		if ($group->grouptype == SUBJECT_GROUP) {
			if ($group->canEdit ()) {
				$url = "dataentry/addlistproject/{$params['entity']->guid}";
				$item = new ElggMenuItem ( 'addlistprojectfile', elgg_echo ( 'Tạo danh sách từ file' ), $url );
				$item->setPriority ( 90 );
				$return [] = $item;
			}
		}
	} else if (elgg_instanceof ( $params ['entity'], 'user' )) {
		
		$current_user = $params ['entity'];
		if ($current_user->isAdmin ()) {
			$url = "dataentry/addgroupsubject/{$current_user->username}";
			$item = new ElggMenuItem ( 'groupsubject', elgg_echo ( 'dataentry:addgroupsubject' ), $url );
			$return [] = $item;
		}
	}
	
	return $return;
}

/*
 * main handle page
 */
function dataentry_page_handler($page) {
	$pages_path = elgg_get_plugins_path () . 'dataentry/pages';
	$group_guid = $page [1];
	$vars ['group_guid'] = $group_guid;
	
	switch ($page [0]) {
		case 'addgroupsubject' :
			elgg_set_page_owner_guid ( $group_guid );
			include "$pages_path/addgroupsubject.php";
			break;
		case 'addsubjectproject' :
			elgg_set_page_owner_guid ( $group_guid );
			include "$pages_path/addsubjectproject.php";
			break;
		
		case "addlistproject" :
			
			include "$pages_path/addlistproject.php";
			break;
		case "addsvncommit" :
			$vars ['group_guid'] = $page [1];
			include "$pages_path/entrysvn.php";
			break;
		default :
			return false;
	}
	
	return true;
}
?>
